import { defineStore } from "pinia";

import type { Course, CourseState } from "@/types/course/course";

export const useCourseStore = defineStore("courseStore", {
  state: (): CourseState => ({
    courses: [],
    total: 0,
    currentPage: 1,
    isLoaded: false,
  }),
  actions: {
    setCourses(courses: Course[], total: number, currentPage: number) {
      // eslint-disable-next-line arrow-parens
      const mappedCourses = courses.map((course) => ({
        ...course,
        img: course.courseImage || "/default-image.jpg", // 映射 courseImage 到 img，添加默认图片
        cardName: course.courseName, // 映射 courseName 到 cardName
        description: course.courseDescription, // 映射 courseDescription 到 description
        views: `${course.viewNum} 已学习`, // 格式化 views
        path: `/course/${course.courseId}`, // 派生路径
        folderId: course.folderId, // 包含 folderId
      }));

      this.courses = mappedCourses;
      this.total = total;
      this.currentPage = currentPage;
      this.isLoaded = true; // 设置为已加载
    },
  },
});
